﻿using System;
using System.Text;
using System.Net.Mail;
using System.Threading;
using System.Globalization;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;

using PriErp.Commands;

namespace PriErp.Frm
{
    public partial class ErrorReport : DevComponents.DotNetBar.Office2007Form
    {
        /// <summary>
        /// 系统出现的异常
        /// </summary>
        private Exception sysException = null;

        public ErrorReport(Exception e)
        {
            InitializeComponent();

            this.sysException = e;
            //设定初始大小
            Height = 370;

            string errinfo = PriErpCommand.GetExceptionMsg(sysException);
            txtErrInfo.Text = errinfo;
        }
        /// <summary>
        /// 错误信息展示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMoreInfo_Click(object sender, EventArgs e)
        {
            if (groupErrInfo.Visible)
            {
                btnMoreInfo.Text = ">>错误信息";
                Height = 370;
            }
            else
            {
                btnMoreInfo.Text = "<<错误信息";
                Height = 540;
            }
            groupErrInfo.Visible = !groupErrInfo.Visible;
            Update();
        }

        private void BtnSubmit_Click(object sender, EventArgs e)
        {
            if (btnSendReport.Checked)
            {
                //发送错误报告
                SendReport();
            }

            if (btnRestart.Checked)
                //重启程序
                Application.Restart();
            else
                this.Close();
        }

        /// <summary>
        /// 发送错误报告
        /// </summary>
        private void SendReport()
        {
            string userInput = edUserInfo.Text,
                   errorInfo = txtErrInfo.Text;

            StringBuilder result = new StringBuilder();
            if (!String.IsNullOrEmpty(userInput))
            {
                result.AppendLine("用户描述：");
                result.AppendLine(userInput);
            }
            result.AppendLine("系统错误：");
            result.AppendLine(errorInfo);

            Thread t = new Thread(delegate()
            {
                PriErpCommand.SendEmail("程序异常错误报告", result.ToString());
            });
            t.Start();
        }
    }
}
